/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CadCliente.java
 *
 * Created on 13/06/2010, 13:43:49
 */
package camada.apresentacao.dialog.carro;

import Relatorios.Cliente.ReportCliente;
import eloma.classe.Cliente;
import camada.bancodedados.daoimplemente.cliente.ClienteDAOImpl;
import camada.bancodedados.daoimplemente.JuridicaDAOImpl;
import camada.bancodedados.daoimplemente.carro.CarroDAOImpl;
import eloma.classe.carro.Carro;
import tabelamodelcolumnmodel.cliente.ClienteColumnModel;
import tabelamodelcolumnmodel.cliente.ClienteTableModel;
import java.awt.Color;
import java.awt.event.KeyEvent;
import java.util.List;
import javax.swing.ListSelectionModel;
import tabelamodelcolumnmodel.carro.CarroColumnModel;
import tabelamodelcolumnmodel.carro.CarroTableModel;
import utilitarios.Exceptions;

/**
 *
 * @author Icaro
 */
public class DialogCarro extends javax.swing.JDialog {

    CarroDAOImpl daoCarro = new CarroDAOImpl();
    Exceptions exceptions = new Exceptions();

    List<Carro> listaCarro;
    private String nomeCliente;
    Carro carro;
    int indexTabela;
    boolean pesquisa = false;

    /** Creates new form CadCliente */
    public DialogCarro(java.awt.Frame parent, boolean modal) {
        
        super(parent, modal);

        initComponents();
        this.setResizable(false);
        this.setLocationRelativeTo(null);
        this.setTitle("Cadastro Veículos");
        configurarTabela(null);
        
    }

    public void configurarTabela(List<Carro> lista) {
        try {

            if (lista == null) {
                listaCarro = daoCarro.selecionarCarro();
            } else {
                listaCarro = lista;
            }

            this.setListaCarro(listaCarro);

            tabelaCarro.setAutoCreateColumnsFromModel(false);
            java.awt.FontMetrics fm = tabelaCarro.getFontMetrics(tabelaCarro.getFont());

            tabelaCarro.setColumnModel(new CarroColumnModel(fm));
            tabelaCarro.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        } catch (Exception e) {
             exceptions.Exceptions(e.getMessage().toString(), "DialogCarro-configurarTabela");
        }
    }

    public void setListaCarro(java.util.List<Carro> listaCarro) {
        this.tabelaCarro.setModel(new CarroTableModel(listaCarro));
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPopupMenu1 = new javax.swing.JPopupMenu();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabelaCarro = new javax.swing.JTable();
        btVoltar = new javax.swing.JButton();
        btAdicionar = new javax.swing.JButton();
        btEditar = new javax.swing.JButton();
        btRemover = new javax.swing.JButton();
        btCancelar = new javax.swing.JButton();
        txPesquisa = new javax.swing.JTextField();
        btPesquisa = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        tabelaCarro.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        tabelaCarro.setToolTipText("Tabela de Clientes Cadastrados");
        tabelaCarro.setSelectionBackground(new java.awt.Color(255, 255, 0));
        tabelaCarro.setSelectionForeground(new java.awt.Color(0, 0, 0));
        tabelaCarro.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tabelaCarroMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tabelaCarro);

        btVoltar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ima/cadastro/Voltar.png"))); // NOI18N
        btVoltar.setText("Voltar");
        btVoltar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btVoltarActionPerformed(evt);
            }
        });

        btAdicionar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ima/cadastro/Add.png"))); // NOI18N
        btAdicionar.setText("Adicionar");
        btAdicionar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btAdicionarActionPerformed(evt);
            }
        });

        btEditar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ima/cadastro/Editar.png"))); // NOI18N
        btEditar.setText("Editar");
        btEditar.setEnabled(false);
        btEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btEditarActionPerformed(evt);
            }
        });

        btRemover.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ima/cadastro/Remove.png"))); // NOI18N
        btRemover.setText("Remover");
        btRemover.setEnabled(false);
        btRemover.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btRemoverActionPerformed(evt);
            }
        });

        btCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ima/cadastro/Cancel.png"))); // NOI18N
        btCancelar.setText("Cancelar");
        btCancelar.setEnabled(false);
        btCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCancelarActionPerformed(evt);
            }
        });

        txPesquisa.setForeground(new java.awt.Color(153, 153, 153));
        txPesquisa.setText("Digite um nome para ser pesquisado");
        txPesquisa.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txPesquisaMouseClicked(evt);
            }
        });
        txPesquisa.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txPesquisaFocusLost(evt);
            }
        });
        txPesquisa.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txPesquisaKeyPressed(evt);
            }
        });

        btPesquisa.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ima/outros/Pesquisa.png"))); // NOI18N
        btPesquisa.setText("Pesquisar");
        btPesquisa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btPesquisaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btAdicionar, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btEditar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btRemover)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btCancelar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btVoltar))
                    .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 638, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(txPesquisa, javax.swing.GroupLayout.PREFERRED_SIZE, 329, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btPesquisa))
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 628, Short.MAX_VALUE))
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btAdicionar)
                    .addComponent(btEditar)
                    .addComponent(btRemover)
                    .addComponent(btCancelar)
                    .addComponent(btVoltar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txPesquisa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btPesquisa))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 262, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tabelaCarroMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tabelaCarroMouseClicked
        try {

             indexTabela = tabelaCarro.getSelectedRow();

             if(indexTabela > -1){

                btAdicionar.setEnabled(false);
                btVoltar.setEnabled(false);
                btEditar.setEnabled(true);
                btRemover.setEnabled(true);
                btCancelar.setEnabled(true);
            }
             
        } catch (Exception e) {
             exceptions.Exceptions(e.getMessage(), "DialogCarro-tabelaCarroMouseClicked");
        }
    }//GEN-LAST:event_tabelaCarroMouseClicked

    private void textDataNascimentoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_textDataNascimentoKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            //textCpf.requestFocus();
        }
    }//GEN-LAST:event_textDataNascimentoKeyPressed

    private void btVoltarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btVoltarActionPerformed
        this.hide();
    }//GEN-LAST:event_btVoltarActionPerformed

    private void btRemoverActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btRemoverActionPerformed
        try{

            CarroTableModel model = (CarroTableModel)tabelaCarro.getModel();
            Carro carro = model.getValoresCarro(indexTabela);

            daoCarro.removerCarro(carro.getIdCarro());
            
            configurarTabela(null);
            
            btAdicionar.setEnabled(true);
            btCancelar.setEnabled(false);
            btEditar.setEnabled(false);
            btRemover.setEnabled(false);
            btVoltar.setEnabled(true);
            
        }catch(Exception e){
             exceptions.Exceptions(e.getMessage(), "DialogCarro-btRemoverActionPerformed");
        }
    }//GEN-LAST:event_btRemoverActionPerformed

    private void btAdicionarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAdicionarActionPerformed
        DialogCadCarro cadCarro = new DialogCadCarro(null, true);
        cadCarro.show();
        configurarTabela(null);
    }//GEN-LAST:event_btAdicionarActionPerformed

    private void txPesquisaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txPesquisaMouseClicked
        if (pesquisa == false) {
            txPesquisa.setText("");
            txPesquisa.setForeground(Color.BLACK);
            pesquisa = true;
        }
    }//GEN-LAST:event_txPesquisaMouseClicked

    private void txPesquisaFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txPesquisaFocusLost
        if(txPesquisa.getText() == null ? "" == null : txPesquisa.getText().equals("")){
            txPesquisa.setForeground(new Color(153,153,153));
            txPesquisa.setText("Digite um nome para ser pesquisado");
            pesquisa = false;
        }
    }//GEN-LAST:event_txPesquisaFocusLost

    private void btPesquisaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btPesquisaActionPerformed
        try {

//            List<Cliente> listaAux = daoCliente.pesquisaClientePorNome(txPesquisa.getText());
//
//            if (listaCliente.size() > 0) {
//
//                for (int j = 0; j < listaCliente.size(); j++) {
//                    
//                    Cliente c = (Cliente) listaCliente.get(j);
//
//                    if (c.getNome().equals(txPesquisa.getText())) {
//                        listaAux.add(c);
//                    }
//                }
//
//                configurarTabela(listaAux);
//                
//            }else{
//
//                List<Cliente> lista = daoCliente.pesquisarTodosClientes();
//
//                configurarTabela(lista);
//            }

        } catch (Exception e) {
            exceptions.Exceptions(e.getMessage(), "DialogCarro-btPesquisaActionPerformed");
        }
    }//GEN-LAST:event_btPesquisaActionPerformed

    private void txPesquisaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txPesquisaKeyPressed
    if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
           btPesquisaActionPerformed(null);
        }
    }//GEN-LAST:event_txPesquisaKeyPressed

    private void btEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btEditarActionPerformed
        try{
            
            CarroTableModel model = (CarroTableModel)tabelaCarro.getModel();
            Carro carro = model.getValoresCarro(indexTabela);
            
            DialogCadCarro cadCarro = new DialogCadCarro(null, true);
            cadCarro.carregarEdicao(carro);
            cadCarro.show();
            configurarTabela(null);
            
        }catch(Exception e){
            exceptions.Exceptions(e.getMessage(), "DialogCarro-btEditarActionPerformed");
        }
    }//GEN-LAST:event_btEditarActionPerformed

    private void btCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCancelarActionPerformed
          btAdicionar.setEnabled(true);
            btCancelar.setEnabled(false);
            btEditar.setEnabled(false);
            btRemover.setEnabled(false);
            btVoltar.setEnabled(true);
    }//GEN-LAST:event_btCancelarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                DialogCarro dialog = new DialogCarro(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btAdicionar;
    private javax.swing.JButton btCancelar;
    private javax.swing.JButton btEditar;
    private javax.swing.JButton btPesquisa;
    private javax.swing.JButton btRemover;
    private javax.swing.JButton btVoltar;
    private javax.swing.JPopupMenu jPopupMenu1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTable tabelaCarro;
    private javax.swing.JTextField txPesquisa;
    // End of variables declaration//GEN-END:variables
}
